package servlet;

import dao.UserDao;
import dao.daoimpl.UserDaoImpl;
import bean.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name="UpdateInformationServlet",urlPatterns = "/UpdateInformationServlet")
public class UpdateInformationServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");/*防止乱码*/
        UserDao userDao=new UserDaoImpl();
        String account=request.getParameter("updateAccount");
        String name=request.getParameter("updateName");
        String screenName=request.getParameter("updateScreenName");
        String sex=request.getParameter("updateSex");
        int age= Integer.parseInt(request.getParameter("updateAge"));
        String birthday=request.getParameter("updateBirthday");
        String address=request.getParameter("updateAddress");
        String phone=request.getParameter("updatePhone");
        String sign=request.getParameter("updateSign");
        String email=request.getParameter("updateEmail");
        String headPortrait=request.getParameter("updateHeadPortrait");
        User user=new User(account,name,screenName,sign,age,sex,phone,address,birthday,email,headPortrait);
        if(userDao.update(user)){
            request.getSession().setAttribute("user",user);
            response.sendRedirect(request.getContextPath()+"/personalHompage.jsp");
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }

}
